package com.zq.zhufengfm.fragments;


import android.os.Bundle;
import android.app.Fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zq.zhufengfm.adapter.BaseFragmentPagerAdapter;
import com.zq.zhufengfm.fragments.download.DownLoadAlbumFragment;
import com.zq.zhufengfm.fragments.download.DownLoadVoiceFragment;
import com.zq.zhufengfm.fragments.download.DownLoadingFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DownLoadTingFragment extends android.support.v4.app.Fragment implements TabLayout.OnTabSelectedListener {

    private ViewPager viewPager ;


    public DownLoadTingFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.base_viewpager_tablayout, container, false);

//        //TabLayout的加载
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.base_tablayout);

        //2.ViewPager的加载
        viewPager = (ViewPager) view.findViewById(R.id.base_viewpager);

        List<BaseFragment> fragments = new ArrayList<BaseFragment>();
        fragments.add(new DownLoadAlbumFragment());
        fragments.add(new DownLoadVoiceFragment());
        fragments.add(new DownLoadingFragment());

        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(
                getChildFragmentManager(), fragments);

        viewPager.setAdapter(pagerAdapter);

        //代码添加： 封装了TabLayout 与ViewPager的联动。
        //需要  ViewPager内部指定的Adapter   必须要重写getPageTitle();
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(this);

        return view;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }
    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }

}
